package offer;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class offerII_0016 {
    public int lengthOfLongestSubstring(String s) {
        int len=s.length();
        int maxlen=0;
        Set<Character> set=new HashSet<>();
        int left=0;
        int right=1;
        if(len==0) return 0;
        set.add(s.charAt(0));
        while(right<len){
            if(set.contains(s.charAt(right))){
                maxlen=Math.max(maxlen,right-left);
                while(set.contains(s.charAt(right))){
                    set.remove(s.charAt(left));
                    left++;
                }
                set.add(s.charAt(right));
            }
            else{
                set.add(s.charAt(right));
            }
            right++;
        }
        maxlen=Math.max(maxlen,right-left);
        return maxlen;
    }
}
